package cn.com.yiqi.wsc.service.subject.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.subject.SubjectDao;
import cn.com.yiqi.wsc.dao.subject.SubjectItemDao;
import cn.com.yiqi.wsc.dao.subject.criteria.SubjectCriteria;
import cn.com.yiqi.wsc.entity.subject.AdminSubjectDTO;
import cn.com.yiqi.wsc.entity.subject.Subject;
import cn.com.yiqi.wsc.entity.subject.SubjectItemDTO;
import cn.com.yiqi.wsc.enumeration.CommonEnum;
import cn.com.yiqi.wsc.enumeration.SubjectTypeEnum;
import cn.com.yiqi.wsc.service.subject.AdminSubjectService;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 何刚
 * @ClassName: ${class}
 * @Description: TODO
 * @date 2017/3/13
 */
@Service
public class AdminSubjectServiceImpl implements AdminSubjectService {

    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private SubjectItemDao subjectItemDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insert(AdminSubjectDTO subjectDTO, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        subjectDTO.setInsertUserId(userId);
        subjectDTO.setEnable(CommonEnum.ENABLE.getValue());

        subjectDao.insert(subjectDTO);


        List<Integer> itemIds = subjectDTO.getItemIds();

        if (!CollectionUtils.isEmpty(itemIds)) {
            List<SubjectItemDTO> subjectItems = new ArrayList<>();

            wrapSubjectItem(subjectDTO, userId, itemIds, subjectItems);
            //添加到subject_item中
            subjectItemDao.batchInsert(subjectItems);
        }


        List<SubjectItemDTO> subjectItems = subjectDTO.getItems();

        if (subjectItems != null && !subjectItems.isEmpty()) {

            warpSubjectDTOList(subjectDTO, userId, subjectItems);

            subjectItemDao.batchInsert(subjectItems);

        }


        return result;
    }

    @Transactional
    @Override
    public Result update(AdminSubjectDTO subjectDTO, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        subjectDTO.setUpdateUserId(userId);
        subjectDao.update(subjectDTO);

        //删除之前的。
        subjectItemDao.deleteBySubjectId(subjectDTO.getId());

        List<Integer> itemIds = subjectDTO.getItemIds();


        if (!CollectionUtils.isEmpty(itemIds)) {
            List<SubjectItemDTO> subjectItems = new ArrayList<>();

            wrapSubjectItem(subjectDTO, userId, itemIds, subjectItems);
            //添加到subject_item中
            subjectItemDao.batchInsert(subjectItems);
        }


        List<SubjectItemDTO> subjectItems = subjectDTO.getItems();
        if (subjectItems != null && !subjectItems.isEmpty()) {

            warpSubjectDTOList(subjectDTO, userId, subjectItems);

            subjectItemDao.batchInsert(subjectItems);

        }


        return result;
    }

    private void warpSubjectDTOList(AdminSubjectDTO subjectDTO, Integer userId, List<SubjectItemDTO> subjectItems) {
        int i = 1;
        for (SubjectItemDTO itemDTO : subjectItems) {
/*            if (itemDTO.getItemId() != null) {
//                itemDTO.setIsItem();
            }*/
            itemDTO.setSubjectId(subjectDTO.getId());
            itemDTO.setInsertUserId(userId);
            itemDTO.setSort(i);
            i++;
        }
    }

    private void wrapSubjectItem(AdminSubjectDTO subjectDTO, Integer userId, List<Integer> itemIds, List<SubjectItemDTO> subjectItems) {
        int i = 1;
        for (Integer id : itemIds) {
            SubjectItemDTO subjectItem = new SubjectItemDTO();
            subjectItem.setInsertUserId(userId);
            subjectItem.setItemId(id);
            subjectItem.setSubjectId(subjectDTO.getId());
            subjectItems.add(subjectItem);
            subjectItem.setSort(i);
            i++;
        }
    }


    @Override
    public Result detail(SubjectCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        AdminSubjectDTO subjectDTO = subjectDao.preview(criteria);

        if (subjectDTO != null) {
            List<String> pic = JSONArray.parseArray(subjectDTO.getBannerUrl(), String.class);

            if (SubjectTypeEnum.CUSTOM.getValue().equals(subjectDTO.getType())) {
                //自定义专题只有一张
                subjectDTO.setBannerUrl(pic.get(0));

            } else {

                subjectDTO.setPicUrls(pic);
                subjectDTO.setBannerUrl(null);

            }
            subjectDTO.setItems(subjectDao.getItemBySubjectId(criteria));
        }

        result.putData("detail", subjectDTO);

        return result;
    }

    @Override
    public Result delete(AdminSubjectDTO subjectDTO, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Subject subject = new Subject();
        subject.setId(subjectDTO.getId());
        subject.setDelete(CommonEnum.DELETED.getValue());
        subject.setUpdateUserId(userId);

        subjectDao.update(subject);

        return result;
    }

    @Override
    public Result pageList(SubjectCriteria criteria) {


        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());
        PageHelper.orderBy("mas.n_sort DESC");
        PageInfo<AdminSubjectDTO> pageInfo = new PageInfo<>(subjectDao.pageList(criteria));

        result.putData("list", pageInfo.getList());
        result.putData("total", pageInfo.getTotal());

        return result;
    }

    @Override
    public Result enable(AdminSubjectDTO subjectDTO, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Subject subject = new Subject();
        subject.setId(subjectDTO.getId());
        subject.setEnable(CommonEnum.ENABLE.getValue());
        subject.setUpdateUserId(userId);

        subjectDao.update(subject);

        return result;
    }

    @Override
    public Result disable(AdminSubjectDTO subjectDTO, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Subject subject = new Subject();
        subject.setId(subjectDTO.getId());
        subject.setEnable(CommonEnum.DISABLE.getValue());
        subject.setUpdateUserId(userId);

        subjectDao.update(subject);

        return result;
    }

    @Transactional
    @Override
    public Result changeSort(Integer bigId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        Integer bigSort = subjectDao.getSortById(bigId);

        if (bigSort == 1) {
            //如果是第一个则不在往上移动了
            return result;
        }

        Integer smallId = subjectDao.getIdBySort(bigSort);

        List<AdminSubjectDTO> subjectDTOList = subjectDao.changeSort(bigId, smallId);

        if (!CollectionUtils.isEmpty(subjectDTOList) && subjectDTOList.size() == 2) {
            //第一个小
            AdminSubjectDTO small = subjectDTOList.get(0);
            //第二个大
            AdminSubjectDTO big = subjectDTOList.get(1);

            Integer tempSort = small.getSort();
            small.setSort(big.getSort());
            big.setSort(tempSort);
            subjectDao.update(small);
            subjectDao.update(big);

        }

        return result;
    }

    @Override
    public Integer getIdBySort(Integer bigId, Integer smallId) {
        return null;
    }
}
